package com.yungam.imcs.controller.auth;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yungam.common.bean.menu.Menu;
import com.yungam.common.bean.menu.RolePermission;
import com.yungam.common.response.RetDTO;
import com.yungam.common.service.MenuService;
import com.yungam.common.service.RolePermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@Api(tags = "菜单接口")
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuService menuService;
    @Resource
    private RolePermissionService permissionService;

    @ApiOperation("获取菜单树")
    @GetMapping("/tree")
    public RetDTO<?> getMenuList() {
        Map<Integer, List<Menu>> map = new HashMap<>();  // { k: parentId, v: Children }
        List<Menu> parentList = new ArrayList<>(); // 所有的父级菜单项
        //
        List<Menu> list = menuService.list();  // 所有菜单项
        for (Menu menu : list) {
            if (menu.getParentId() == 0) {
                parentList.add(menu);
            } else {
                List<Menu> children = map.getOrDefault(menu.getParentId(), new ArrayList<>());
                children.add(menu);
                map.put(menu.getParentId(), children);
            }
        }
        for (Menu fa : parentList) {
            if (map.containsKey(fa.getId())) {
                List<Menu> child = map.get(fa.getId());
                child.sort((x, y) -> (Integer) (x.getWeight() - y.getWeight())); // 按照权重排序
                fa.setSubItems(child);
            }
        }
        parentList.sort((x, y) -> (Integer) (x.getWeight() - y.getWeight())); // 按照权重排序
        return RetDTO.data(parentList);
    }

    @ApiOperation("查询某一角色的菜单")
    @GetMapping("/getMenuByRole")
    public RetDTO<?> getMenuByRole(@ApiParam("角色 id") Integer id) {
        QueryWrapper<RolePermission> qw = new QueryWrapper<>();
        qw.eq("role_id", id);
        List<RolePermission> list = permissionService.list(qw);
        return RetDTO.data(list);
    }

    @ApiOperation("更新某一角色的菜单")
    @PostMapping("/updateMenuByRole")
    public RetDTO<?> updateMenuByRole(@ApiParam("{ roleId: 角色 id, menus: 菜单 id 集合 }")
                                          @RequestBody Map<String, Object> map) {
        Integer roleId = (Integer) map.get("roleId");
        List<Integer> menus = (List<Integer>) map.get("menus");
        QueryWrapper<RolePermission> qw = new QueryWrapper<>();
        qw.eq("role_id", roleId);
        permissionService.remove(qw);
        Date date = new Date();
        for (Integer menu : menus) {
            RolePermission rp = new RolePermission();
            rp.setRoleId(roleId)
                    .setMenuId(menu)
                    .setCreateTime(date);
            permissionService.save(rp);
        }
        return RetDTO.success();
    }
}
